from django.http import Http404
from rest_framework.response import Response
from rest_framework import status
from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView
from .serializers import UserInfoSerializer, AddrSerializer
from .models import UserInfo, Addr


class UserListView(GenericAPIView):
    queryset = UserInfo.objects.all()
    serializer_class = UserInfoSerializer

    def get(self, request, format=None):
        # 获取信息
        # 查询所有用户信息
        users = self.get_queryset()
        # 创建序列化器对象
        ser = self.get_serializer(users, many=True)

        result = {"data": ser.data, "code": 200, "message": "ok", }
        return Response(result, status=status.HTTP_200_OK)

    def post(self, request, format=None):
        # 添加一条信息
        ser = self.get_serializer(data=request.data)
        if ser.is_valid():
            ser.save()
            data = {'code': 201, "data": ser.data, "message": "ok"}
            return Response(data, status=status.HTTP_201_CREATED)
        else:
            data = {'code': 400, "message": ser.errors}
            return Response(data, status=status.HTTP_400_BAD_REQUEST)


class UserDetailView(GenericAPIView):
    queryset = UserInfo.objects.all()
    serializer_class = UserInfoSerializer

    def get(self, request, id, format=None):
        obj = self.get_object()
        ser = self.get_serializer(obj)
        data = {'code': 200, "data": ser.data, "message": "ok"}
        return Response(data, status=status.HTTP_200_OK)

    def delete(self, request, id, format=None):
        obj = self.get_object()
        obj.delete()
        data = {'code': 204, "message": "删除成功"}
        return Response(data, status=status.HTTP_204_NO_CONTENT)

    def put(self, request, id, format=None):
        obj = self.get_object()
        ser = self.get_serializer(instance=obj, data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code': 200, "data": ser.data, "message": "ok"}, status=status.HTTP_200_OK)
        else:
            return Response({'code': 400, "message": ser.errors}, status=status.HTTP_400_BAD_REQUEST)


# 收货地址增删查改
class AddrListView(GenericAPIView):
    queryset = Addr.objects.all()
    serializer_class = AddrSerializer

    def get(self, request, format=None):
        # 获取信息
        # 查询所有用户信息
        # 创建序列化器对象
        ser = self.get_serializer(self.get_queryset(), many=True)

        result = {"data": ser.data, "code": 200, "message": "ok", }
        return Response(result, status=status.HTTP_200_OK)

    def post(self, request, format=None):
        # 添加一条信息
        ser = self.get_serializer(data=request.data)
        if ser.is_valid():
            ser.save()
            data = {'code': 201, "data": ser.data, "message": "ok"}
            return Response(data, status=status.HTTP_201_CREATED)
        else:
            data = {'code': 400, "message": ser.errors}
            return Response(data, status=status.HTTP_400_BAD_REQUEST)


class AddrDetailView(GenericAPIView):
    queryset = Addr.objects.all()
    serializer_class = AddrSerializer

    def get(self, request, id, format=None):
        obj = self.get_object()
        ser = self.get_serializer(obj)
        data = {'code': 200, "data": ser.data, "message": "ok"}
        return Response(data, status=status.HTTP_200_OK)

    def delete(self, request, id, format=None):
        obj = self.get_object()
        obj.delete()
        data = {'code': 204, "message": "删除成功"}
        return Response(data, status=status.HTTP_204_NO_CONTENT)

    def put(self, request, id, format=None):
        obj = self.get_object()
        ser = self.get_serializer(instance=obj, data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code': 200, "data": ser.data, "message": "ok"}, status=status.HTTP_200_OK)
        else:
            return Response({'code': 400, "message": ser.errors}, status=status.HTTP_400_BAD_REQUEST)
